package parser

import (
	"gitee.com/jnshao/go-start/projects/crawler/engine"
	"regexp"
)

var cityRe = regexp.MustCompile(`<th><a href="(http://album.zhenai.com/u/[0-9]+)" target="_blank">([^"]+)</a></th>`)

var cityUrlRe = regexp.MustCompile(`"(http://www.zhenai.com/zhenghun/[^"]+)"`)

var idUrlRe = regexp.MustCompile(`http://album.zhenai.com/u/([0-9]+)`)

func ParseCity(content []byte) engine.ParserResult {
	result := engine.NilParser(nil)
	matches := cityRe.FindAllSubmatch(content, -1)
	for _, submatchs := range matches {
		username := string(submatchs[2])
		idmatchs := idUrlRe.FindSubmatch(submatchs[1])
		url := string(submatchs[1])
		item := engine.Item{
			Url:     url,
			Type:    "zhenai",
			Id:      string(idmatchs[1]),
			Payload: PraseProfile(content, username),
		}
		result.Items = append(result.Items, item)
	}

	urlMatches := cityUrlRe.FindAllSubmatch(content, -1)
	for _, urlSubMatches := range urlMatches {
		f := engine.NewFuncParser(ParseCity, "ParseCity", nil)
		result.Requests = append(result.Requests, engine.Request{
			URL:    string(urlSubMatches[1]),
			Parser: &f,
		})
	}

	return result
}
